//
//  SISSTenancyRentOutTakeLookCell.m
//  SISSAPP
//
//  Created by 袁应荣 on 2018/1/5.
//  Copyright © 2018年 优房网. All rights reserved.
//

#import "SISSTenancyRentOutTakeLookCell.h"
#import "SISSRentOutTakeLookModel.h"


@interface SISSTenancyRentOutTakeLookCell ()

/// 称呼Label
@property (weak, nonatomic) UILabel *nameLabel;
/// 需求信息Label
@property (weak, nonatomic) UILabel *infoLabel;
/* labelsView */
@property (weak, nonatomic) UIView *labelsView;
@property (strong, nonatomic) NSArray<UILabel *> *markLabels;
/// 选择按钮
@property (weak, nonatomic) UIButton *checkButton;
/// 聊天按钮
@property (weak, nonatomic) UIButton *chatButton;
/// 看房时间Label
@property (weak, nonatomic) UILabel *timeTitleLabel;
/// 时间Label
@property (weak, nonatomic) UILabel *timeLabel;
/// 看房记录标签Label
@property (weak, nonatomic) UILabel *tagLabel;
/// 底部线条
@property (weak, nonatomic) UIView *lineView;

/// 状态图片
@property (weak, nonatomic) UIImageView *stateImageView;

@end
@implementation SISSTenancyRentOutTakeLookCell


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setRentOutTakeLookModel:(SISSRentOutTakeLookModel *)rentOutTakeLookModel {
    _rentOutTakeLookModel = rentOutTakeLookModel;
    self.checkButton.selected = rentOutTakeLookModel.isSelected;
  
    self.nameLabel.text = [NSString stringWithFormat:@"%@ 求租",rentOutTakeLookModel.renterName];
    self.timeLabel.text = [rentOutTakeLookModel.takelookDate substringToIndex:10];
    self.infoLabel.hidden = !rentOutTakeLookModel.rent;
    self.infoLabel.text = [NSString stringWithFormat:@"%@-%@ | %@-%@ | %@",rentOutTakeLookModel.rent.minPrice,rentOutTakeLookModel.rent.maxPrice,rentOutTakeLookModel.rent.region,rentOutTakeLookModel.rent.regionDetail,rentOutTakeLookModel.rent.houseType];
    self.infoLabel.hidden = rentOutTakeLookModel.businessType == 2;
    NSArray *houseFeatureArr = rentOutTakeLookModel.rent.houseFeature.length == 0 ? @[] : [rentOutTakeLookModel.rent.houseFeature componentsSeparatedByString:@"#"];
    for (UILabel *label in self.markLabels) {
        label.hidden = YES;
    }
    
    for (NSInteger i = 0; i < houseFeatureArr.count ; i++) {
        UILabel *label = self.markLabels[i];
        label.hidden = NO;
        label.text = houseFeatureArr[i];
        label.layer.borderWidth = 1;
        label.text = [NSString stringWithFormat:@"%@",houseFeatureArr[i]];
        UIColor *color = [SISSPublicMethods matchTextColorWithHouseFeatureStr:houseFeatureArr[i]];
        label.layer.borderColor = color.CGColor;
        label.textColor = color;
        label.layer.cornerRadius = 2;
        label.layer.masksToBounds = YES;
    }
    
    [self.labelsView mas_updateConstraints:^(MASConstraintMaker *make) {
        if (rentOutTakeLookModel.rent.houseFeature.length == 0) {
            if (rentOutTakeLookModel.businessType == 2) {
                make.height.mas_equalTo(0);
            } else {
                make.height.mas_equalTo(36);
            }
        } else {
            make.height.mas_equalTo(self.markLabels[houseFeatureArr.count - 1].frame.origin.y + 30);
        }
        
    }];
    
    NSString *tagStr;
    UIColor *tagColor;
    if (rentOutTakeLookModel.rent.rentType == 1) { // 线上
        if (rentOutTakeLookModel.landlordState == 0 && rentOutTakeLookModel.renterState == 0) {
            tagStr = @"待确认";
            tagColor = COLOR(0xFF1F1D);
            self.checkButton.userInteractionEnabled = NO;
        } else if (rentOutTakeLookModel.landlordState == 2 || rentOutTakeLookModel.renterState == 2) {
            tagStr = rentOutTakeLookModel.landlordState == 2 ? @"出租方否认看房" : @"承租方否认看房";
            tagColor = COLOR(0xFF1F1D);
            self.checkButton.userInteractionEnabled = NO;
        } else if (rentOutTakeLookModel.landlordState == 1 && rentOutTakeLookModel.renterState == 1) {
            tagStr = @"";
        } else if (rentOutTakeLookModel.landlordState == 1 || rentOutTakeLookModel.renterState == 1) {
            tagStr = rentOutTakeLookModel.landlordState == 1 ? @"等待出租方确认" : @"等待承租方确认";
            tagColor = COLOR(0xFF1F1D);
            self.checkButton.userInteractionEnabled = NO;
        }
    } else if (rentOutTakeLookModel.rent.rentType == 2) { // 线下
        
        switch (rentOutTakeLookModel.landlordState) {
            case 0:
                tagStr = @"待确认";
                tagColor = COLOR(0xFF1F1D);
                self.checkButton.userInteractionEnabled = NO;
                break;
                
            case 1:
                tagStr = @"";
                tagColor = COLOR(0xFF1F1D);
                break;
                
            case 2:
                tagStr = @"已否认";
                tagColor = COLOR(0xFF1F1D);
                self.checkButton.userInteractionEnabled = NO;
                break;
                
            default:
                break;
        }
    }
    
    // 签约中
    self.tagLabel.textColor = tagColor;
    self.tagLabel.text = tagStr;
    if (rentOutTakeLookModel.rent.traded == 1) { // || rentOutTakeLookModel.trading == 1 || takeLookModel.deleted == 1
        NSString *imageName = @"";
//        if (rentOutTakeLookModel.deleted == 1) {
//            imageName = @"已失效";
//        }
//        if (rentOutTakeLookModel.trading == 1) {
//            imageName = @"签约中";
//        }
        if (rentOutTakeLookModel.rent.traded == 1) {
            imageName = @"已签约";
        }
        
        self.stateImageView.image = [UIImage imageNamed:imageName];
        self.infoLabel.textColor = SECONDARY_TEXT_COLOR;
        self.timeTitleLabel.textColor = SECONDARY_TEXT_COLOR;
        self.timeLabel.textColor = SECONDARY_TEXT_COLOR;
        self.tagLabel.textColor = SECONDARY_TEXT_COLOR;
        self.checkButton.userInteractionEnabled = NO;
    }
}

- (void)checkButtonClickAction:(UIButton *)button {
    if ([self.delegate respondsToSelector:@selector(tenancyRentOutTakeLookRecordCell:checkButtonIsSelected:)]) {
        BOOL doSelection = [self.delegate tenancyRentOutTakeLookRecordCell:self checkButtonIsSelected:!self.checkButton.isSelected];
        if(!doSelection){
            
        } else {
            self.rentOutTakeLookModel.isSelected = !self.rentOutTakeLookModel.isSelected;
            self.checkButton.selected = self.rentOutTakeLookModel.isSelected;
        }
    }
}

- (void)chatButtonClickAction:(UIButton *)button {
    
    if ([self.delegate respondsToSelector:@selector(tenancyRentOutTakeLookRecordChatButtonClickWithCell:)]) {
        [self.delegate tenancyRentOutTakeLookRecordChatButtonClickWithCell:self];
    }
}

#pragma mark - UI
- (void)initUI {
    [super initUI];
    
    UILabel *nameLabel = [[UILabel alloc] init];
    nameLabel.textColor = MAIN_TEXT_COLOR;
    nameLabel.font = [UIFont systemFontOfSize:13];
    nameLabel.text = @"张先生 求租";
    [self.contentView addSubview:nameLabel];
    self.nameLabel = nameLabel;
    
    UIButton *checkButton = [UIButton buttonWithType:UIButtonTypeCustom];
    checkButton.tag = 1;
    [checkButton setImage:[UIImage imageNamed:@"check"] forState:UIControlStateNormal];
    [checkButton setImage:[UIImage imageNamed:@"mainCheckSelected"] forState:UIControlStateSelected];
    [checkButton addTarget:self action:@selector(checkButtonClickAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:checkButton];
    self.checkButton = checkButton;
    
    UIButton *chatButton = [UIButton buttonWithType:UIButtonTypeCustom];
    chatButton.tag = 2;
    [chatButton setImage:[UIImage imageNamed:@"咨询"] forState:UIControlStateNormal];
    [chatButton addTarget:self action:@selector(chatButtonClickAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:chatButton];
    self.chatButton = chatButton;
    
    UIView *labelsView = [[UIView alloc] init];
    labelsView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    labelsView.layer.cornerRadius = 4;
    [self.contentView addSubview:labelsView];
    self.labelsView = labelsView;
    
    UILabel *infoLabel = [[UILabel alloc] initWithFrame:CGRectMake(8, 0, SCREEN_WIDTH, 28)];
    infoLabel.textColor = MAIN_TEXT_COLOR;
    infoLabel.font = [UIFont systemFontOfSize:13];
    infoLabel.text = @"租金预算 | 意向区域 | 户型";
    [self.labelsView addSubview:infoLabel];
    self.infoLabel = infoLabel;
    
    NSMutableArray<UILabel *> *arrM = [NSMutableArray arrayWithCapacity:6];
    float labelX = 8;
    float labelY = 36;
    CGFloat labelWidth = 66;
    for (NSInteger i = 0; i < 6; i++) {
        if (labelX + labelWidth + 20 > SCREEN_WIDTH) {
            labelX = 8;
            labelY += 36;
        }
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(labelX, labelY, labelWidth, 28)];
        label.textColor = SECONDARY_TEXT_COLOR;
        label.font = [UIFont systemFontOfSize:13];
        label.textAlignment = NSTextAlignmentCenter;
        label.text = @"租房要求";
        label.layer.borderWidth = 1;
        label.layer.cornerRadius = 4;
        label.layer.borderColor = SECONDARY_TEXT_COLOR.CGColor;
//        label.layer.masksToBounds = YES;
        [arrM addObject:label];
        [self.labelsView addSubview:label];
        
        labelX = CGRectGetMaxX(label.frame) + 10;
        label.hidden = YES;
    }
    self.markLabels = arrM;
    
    UILabel *timeTitleLabel = [[UILabel alloc] init];
    timeTitleLabel.textColor = MAIN_TEXT_COLOR;
    timeTitleLabel.font = [UIFont systemFontOfSize:13];
    timeTitleLabel.text = @"预约时间：";
    [self.contentView addSubview:timeTitleLabel];
    self.timeTitleLabel = timeTitleLabel;
    
    UILabel *timeLabel = [[UILabel alloc] init];
    timeLabel.textColor = MAIN_TEXT_COLOR;
    timeLabel.font = [UIFont systemFontOfSize:13];
    timeLabel.text = @"时间";
    [self.contentView addSubview:timeLabel];
    self.timeLabel = timeLabel;
    
    UILabel *tagLabel = [[UILabel alloc] init];
    tagLabel.textColor = SECONDARY_TEXT_COLOR;
    tagLabel.font = [UIFont systemFontOfSize:13];
    tagLabel.text = @"看房记录标签";
    [self.contentView addSubview:tagLabel];
    self.tagLabel = tagLabel;
    
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.contentView addSubview:lineView];
    self.lineView = lineView;
    
    UIImageView *stateImageView = [[UIImageView alloc] init];
    [self.contentView addSubview:stateImageView];
    self.stateImageView = stateImageView;
}


#pragma mark - 约束
- (void)initConstraint {
    [super initConstraint];
    
    CGFloat margin = 8.0;
    WeakSelf
    
    [self.checkButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(weakSelf.contentView);
        make.left.equalTo(weakSelf.contentView).offset(2 * margin);
    }];
    
    [self.chatButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.contentView).offset(2 * margin);
        make.right.equalTo(weakSelf.contentView).offset(-2 * margin);
    }];
    
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.checkButton.mas_right).offset(margin);
        make.top.equalTo(weakSelf.contentView).offset(2 * margin);
    }];
    
    [self.labelsView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.checkButton.mas_right).offset(margin);
        make.right.equalTo(weakSelf.contentView).offset(-2 * margin);
        make.top.equalTo(weakSelf.nameLabel.mas_bottom).offset(margin);
        make.height.mas_equalTo(weakSelf.markLabels[weakSelf.markLabels.count - 1].frame.origin.y + 36);
    }];
    
    [self.timeTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.nameLabel);
        make.top.equalTo(weakSelf.labelsView.mas_bottom).offset(2 * margin);
        make.bottom.equalTo(weakSelf.lineView).offset(-2 * margin);
    }];
    
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.timeTitleLabel.mas_right);
        make.centerY.equalTo(weakSelf.timeTitleLabel);
    }];
    
    [self.tagLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(weakSelf.contentView);
        make.right.equalTo(weakSelf.contentView).offset(-2 * margin);
    }];
    
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(weakSelf.contentView);
        make.height.mas_equalTo(1);
    }];

    [self.stateImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.checkButton.mas_right).offset(2 * margin);
        make.centerY.equalTo(weakSelf.contentView);
        make.size.mas_equalTo(CGSizeMake(66, 66));
    }];
}

@end
